var totalPage; //总页码
var currentPage = 1; //当前页
var pageSize = 4; //每页显示条数
bindPage(); //生成页码
addPageEvent(); //显示某页的数据
displayTbody(); //显示
addNewDep(); //添加
delDep(); //删除
modifyDep(); //修改
queryDep(); //查询


function displayTbody() { //显示
    var depArr = downLoad(depArr); //拉取
    console.log(depArr);
    $("#ftbody").html("");
    var start = (currentPage - 1) * pageSize; //每页开始
    var end = start + pageSize; //每页结束
    for (var i = start; i < end && i < depArr.length; i++) {
        $("#ftbody").append(
            `<tr>
            <td class=fdepId>${depArr[i].depId}</td>
            <td class=fdepName>${depArr[i].depName}</td>
            <td class=fdepEstablishTime>${depArr[i].depEstablishTime}</td>
            <td class=fepDescribe>${depArr[i].depDescribe}</td>
            <td><button type="button" class = "fdelete btn btn-primary" data-Id = '${depArr[i].depId}'>删除</button><button class = "alter btn btn-success" type="button" data-xg = '${i}'>修改</button></td>
            </tr>`
        )
    }
}
//添加
function addNewDep() {
    $('#addShuJu').click(function() {
        $("#addmodal").modal('show');
    })
    $('#newDepName').blur(function() {
        if ($(this).val().length != 0) {
            $("#qsrName").text("");
            $("#qsrName").addClass("newInput");
        } else {
            $("#qsrName").text("请输入内容");
            $("#qsrName").removeClass("newInput");
            $("#qsrName").addClass("newInputtext");

        }
    })
    $('#newDepTime').blur(function() {
        if ($(this).val().length == 0) {
            $("#qsrTime").removeClass("newInput");
            $("#qsrTime").addClass("newInputtext");
            $("#qsrTime").text("请输入内容")
        } else {
            $("#qsrTime").addClass("newInput");
            $("#qsrTime").text("");
        }

    })
    $('#newDepDescribe').blur(function() {
        if ($('#newDepDescribe').val().length == 0) {
            $("#qsrDescribe").removeClass("newInput");
            $("#qsrDescribe").addClass("newInputtext");
            $("#qsrDescribe").text("请输入内容")
        } else {
            $("#qsrDescribe").addClass("newInput");
            $("#qsrDescribe").text("");
        }

    })
    $("#addNewDepBtn").click(function() {
            var depArr = downLoad(depArr); //拉取
            var newDepId = Number(depArr[depArr.length - 1].depId) + 1;
            var newDepName = $("#newDepName").val();
            var newDepTime = $("#newDepTime").val();
            var newDepDescribe = $("#newDepDescribe").val();
            var newDepInformation = { "depId": newDepId, "depName": newDepName, "depEstablishTime": newDepTime, "depDescribe": newDepDescribe };
            if (newDepName != "" && newDepName != null && newDepTime != "" && newDepTime != null && newDepDescribe != "" && newDepDescribe != null) {
                depArr.push(newDepInformation); //加入数组
                upLoad(depArr, depArr); //上传
                $("#addCg").modal('show');
                bindPage(); //生成页码
                displayTbody(); //显示
                emptyNewDep(); //清空添加框的input的内容
                $("#qsrName").text("");
                $("#qsrName").removeClass("newInput");
                $("#qsrName").removeClass("newInputtext");

                $("#qsrTime").text("");
                $("#qsrTime").removeClass("newInput");
                $("#qsrTime").removeClass("newInputtext");

                $("#qsrDescribe").text("");
                $("#qsrDescribe").removeClass("newInput");
                $("#qsrDescribe").removeClass("newInputtext");
            } else {
                $('#addSb').modal('show');

            }
        })
        //取消添加
    $('#cancelAddBtn').click(function() {
        $('#addmodal').modal('hide');
        $("#qsrName").text("");
        $("#qsrName").removeClass("newInput");
        $("#qsrName").removeClass("newInputtext");

        $("#qsrTime").text("");
        $("#qsrTime").removeClass("newInput");
        $("#qsrTime").removeClass("newInputtext");

        $("#qsrDescribe").text("");
        $("#qsrDescribe").removeClass("newInput");
        $("#qsrDescribe").removeClass("newInputtext");
        emptyNewDep();
    })


}
//清空添加框的input的内容
function emptyNewDep() {
    $("#newDepName").val("");
    $("#newDepTime").val("");
    $("#newDepDescribe").val("");
}
//删除
function delDep() {
    $("#ftbody").on('click', '.fdelete', function() {
        $('#detDel').modal('show');
        var index = $(this).parent().parent();
        var id = $(this).attr("data-Id");
        $("#determineDel").click(function() {
            index.remove();
            delDepArray(id);
            $('#deleteSuccessModal').modal('show');
            bindPage(); //重新生成页码
            displayTbody(); //显示
        })
        $("#determineCancel").click(function() {
            $('#detDel').modal('hide');
        })
    })
}
//删除对应数组
function delDepArray(id) {
    var depArr = downLoad(depArr); //拉取
    var employeeArr = upEmp(employeeArr); //拉取员工信息数组
    // console.log(employeeArr);
    for (var i = 0; i < depArr.length; i++) {
        if (depArr[i].depId == id) {
            for (var j = 0; j < employeeArr.length; j++) {
                //找到该部门的员工
                if (depArr[i].depId == employeeArr[j].eDepId) {
                    //将该部门员工的部门编号变成000，即为待定
                    employeeArr[j].eDepId = "000";
                    downEmp(employeeArr, employeeArr); //上传员工信息数据
                }
            }
            depArr.splice(i, 1);
            break;
        }
    }
    upLoad(depArr, depArr); //上传
}

//修改
function modifyDep() {
    var depArr = downLoad(depArr); //拉取
    var xiugai;
    $("#ftbody").on('click', '.alter', function() {
            $("#correct").modal('show');
            var index = $(this).attr('data-xg');
            xiugai = index;
            $('#modDepName').val(depArr[index].depName);
            $('#modDepTime').val(depArr[index].depEstablishTime);
            $('#modDepDescribe').val(depArr[index].depDescribe);
        })
        // 判断是否输入值
    $('#modDepName').blur(function() {
        if ($(this).val().length == 0) {
            $("#modqsrName").removeClass("newInput");
            $("#modqsrName").addClass("newInputtext");
            $("#modqsrName").text("请输入内容")
        } else {
            $("#modqsrName").addClass("newInput");
            $("#modqsrName").text("");
        }

    })
    $('#modDepTime').blur(function() {
        if ($(this).val().length == 0) {
            $("#modqsrTime").removeClass("newInput");
            $("#modqsrTime").addClass("newInputtext");
            $("#modqsrTime").text("请输入内容")
        } else {
            $("#modqsrTime").addClass("newInput");
            $("#modqsrTime").text("");
        }

    })
    $('#modDepDescribe').blur(function() {
        if ($(this).val().length == 0) {
            $("#qsrDescribemod").removeClass("newInput");
            $("#qsrDescribemod").addClass("newInputtext");
            $("#qsrDescribemod").text("请输入内容")
        } else {
            $("#qsrDescribemod").addClass("newInput");
            $("#qsrDescribemod").text("");
        }

    })
    $("#correct").on('click', '#modDepBtn', function() {
            if ($('#modDepName').val() != "" && $('#modDepName').val() != null && $('#modDepTime').val() != "" && $('#modDepTime').val() != null && $('#modDepDescribe').val() != "" && $('#modDepDescribe').val() != null) {
                depArr[xiugai].depName = $('#modDepName').val();
                depArr[xiugai].depEstablishTime = $('#modDepTime').val();
                depArr[xiugai].depDescribe = $('#modDepDescribe').val();
                upLoad(depArr, depArr); //上传
                $('#modifySuccess').modal('show');
                displayTbody();
                emptyModDep();
            } else {
                $('#modifyFailMod').modal('show');
            }

        })
        //取消修改
    $("#cancelmodDep").click(function() {
        $('#correct').modal('hide');

        $("#modqsrName").text("");
        $("#modqsrName").removeClass("newInput");
        $("#modqsrName").removeClass("newInputtext");

        $("#modqsrTime").text("");
        $("#modqsrTime").removeClass("newInput");
        $("#modqsrTime").removeClass("newInputtext");

        $("#qsrDescribemod").text("");
        $("#qsrDescribemod").removeClass("newInput");
        $("#qsrDescribemod").removeClass("newInputtext");
    })
}
//清空修改框的input的内容
function emptyModDep() {
    $("#modDepName").val("");
    $("#modDepTime").val("");
    $("#modDepDescribe").val("");
}
//查询
function queryDep() {

    $('#queryDep').on('input', function() {
        var query = $('#queryDep').val();
        if (query == "") {
            displayTbody();
        } else {
            $('#ftbody').html("");
            var depArr = downLoad(depArr); //拉取
            for (var i = 0; i < depArr.length; i++) {
                if (depArr[i].depName.indexOf(query) != -1 || depArr[i].depEstablishTime.indexOf(query) != -1 || depArr[i].depDescribe.indexOf(query) != -1) {
                    $("#ftbody").append(
                        `<tr>
                        <td class=fdepId>${depArr[i].depId}</td>
                        <td class=fdepName>${depArr[i].depName}</td>
                        <td class=fdepEstablishTime>${depArr[i].depEstablishTime}</td>
                        <td class=fepDescribe>${depArr[i].depDescribe}</td>
                        <td><button class = 'fdelete btn btn-primary' data-Id = '${depArr[i].depId}'>删除</button><button class = 'alter btn btn-success' data-xg = '${i}'>修改</button></td>
                        </tr>`
                    )
                }
            }
            for (var i in depArr) {
                if (depArr[i].depId == query) {
                    $('#ftbody').html("");
                    $('#ftbody').append(`
                    <tr>
                        <td class=fdepId>${depArr[i].depId}</td>
                        <td class=fdepName>${depArr[i].depName}</td>
                        <td class=fdepEstablishTime>${depArr[i].depEstablishTime}</td>
                        <td class=fepDescribe>${depArr[i].depDescribe}</td>
                        <td><button class = 'fdelete btn btn-primary' data-Id = '${depArr[i].depId}'>删除</button><button class = 'alter btn btn-success' data-xg = '${i}'>修改</button></td>
                        </tr>`)
                }
            }
        }
    })
}
//分页
//显示当前页的数据
function addPageEvent() {
    $("#divPage").on('click', '.btn', function() {
        currentPage = Number($(this).text());
        displayTbody(); //显示
    })
}
//生成页码
function bindPage() {
    var depArr = downLoad(depArr); //拉取
    var totalSize = depArr.length; //总数量
    totalPage = Math.ceil(totalSize / pageSize); //总页数
    if (totalPage < currentPage) { //如果总页数小于当前页
        currentPage = totalPage;
    }
    $("#divPage").html("");
    for (var i = 1; i <= totalPage; i++) {
        $("#divPage").append(`<button class="btn btn-default">${i}</button>`);
    }
}
//上传
function upLoad(depArr, depArr) {
    var depArrr = JSON.stringify(depArr);
    localStorage.setItem("depArr", depArrr);
}
//拉取
function downLoad(depArr) {
    var dep = localStorage.getItem("depArr");
    var depArr = JSON.parse(dep);
    return depArr;
}